package com.cate.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.cate.base.BasePageVO;
import com.cate.constants.UserConstant;
import com.cate.context.BaseContext;
import com.cate.pojo.dto.EmployeeEditPasswordDTO;
import com.cate.pojo.dto.EmployeeInfoDTO;
import com.cate.pojo.dto.EmployeeLoginDTO;
import com.cate.pojo.dto.EmployeePageDTO;
import com.cate.pojo.entity.Employee;
import com.cate.exception.BusinessException;
import com.cate.mapper.EmployeeMapper;
import com.cate.result.SystemCode;
import com.cate.service.EmployeeService;
import com.cate.utils.JasyptUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @CreateTime: 2023/7/26
 * @Author: <==lhd==>
 * @Description: 员工管理
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        final String username = employeeLoginDTO.getUsername();
        return employeeMapper.getByUsername(username);
    }

    @Override
    public void editPassword(EmployeeEditPasswordDTO employeeEditPasswordDTO) {

        // 获得登录员工ID和员工信息
        final Long empId = BaseContext.getCurrentId();
        final Employee employee = employeeMapper.getById(empId);

        if (employee == null) {
            throw new BusinessException(SystemCode.INNER_ERROR);
        }

        // 查询原密码是否正确
        if (!JasyptUtil.checkPassword(employeeEditPasswordDTO.getOldPassword(), employee.getPassword())) {
            throw new BusinessException(SystemCode.PARAMETER_VALID_ERROR, "原密码不正确");
        }
        // 修改密码
        String newPassword = JasyptUtil.encryptPassword(employeeEditPasswordDTO.getNewPassword());
        final boolean isDone = employeeMapper.editPassword(employee.getId(), newPassword);

        if (!isDone) {
            throw new BusinessException(SystemCode.INNER_ERROR,"修改密码失败");
        }
    }

    @Override
    public void setStatus(Integer status,Long id) {
        final boolean isDone = employeeMapper.setStatus(id, status);
        if(!isDone){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"修改账号状态失败");
        }
    }

    @Override
    public BasePageVO<Employee> queryPage(EmployeePageDTO employeePageDTO) {
        PageHelper.startPage(employeePageDTO.getPage(),employeePageDTO.getPageSize());

        Page<Employee> employeePage = employeeMapper.selectList(employeePageDTO.getName());

        if(employeePage==null){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"分页请求失败");
        }
        return new BasePageVO<>(employeePage.getTotal(), employeePage.getResult());
    }


    @Override
    public void save(EmployeeInfoDTO employeeInfoDTO) {
        // 判断账号是否已经存在
        final Employee getEmployee = employeeMapper.getByUsername(employeeInfoDTO.getUsername());

        if(getEmployee!=null){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"用户已经存在");
        }

        final Employee employee = BeanUtil.copyProperties(employeeInfoDTO, Employee.class);
        employee.setPassword(JasyptUtil.encryptPassword(UserConstant.DEFAULT_Password));
        employee.setStatus(1);
        final boolean isDone = employeeMapper.insertOne(employee);
        if(!isDone){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"保存失败");
        }
    }

    @Override
    public Employee getById(Long id) {
        final Employee employee = employeeMapper.getById(id);
        if(employee==null){
            throw new BusinessException(SystemCode.PARAMETER_VALID_ERROR,"无该用户");
        }
        return employee;
    }

    @Override
    public void update(EmployeeInfoDTO employeeInfoDTO) {
        final Employee employee = BeanUtil.copyProperties(employeeInfoDTO, Employee.class);
        final boolean isDone = employeeMapper.update(employee);
        if(!isDone){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"更新失败");
        }
    }
}
